package com.junxonline.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author JunX
 * @ClassName: RedisUtils
 * @Description: Redis工具类
 */

@Component
public class RedisUtils<T> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 一天有多少分钟，默认时间是一天
     */
    private static final long MINUTES_OF_ONE_DAY = 24 * 60;


    /**
     * 将 key，value 存放到redis数据库中，默认设置过期时间为一天
     *
     * @param key
     * @param value
     */
    public void set(String key, T value) {
        set(key, value, MINUTES_OF_ONE_DAY);
    }

    /**
     * 将 key，value 存放到redis数据库中，设置过期时间单位是分钟
     *
     * @param key
     * @param value
     * @param expireTime 单位是秒
     */
    public void set(String key, T value, long expireTime) {
        ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, expireTime, TimeUnit.MINUTES);
    }

    /**
     * 将 hash，key，value 存放到redis数据库中，默认设置过期时间为一天
     *
     * @param hash
     * @param key
     * @param value
     */
    public void put(String hash, String key, T value) {
        HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(hash, key, value);
        redisTemplate.expire(key, MINUTES_OF_ONE_DAY, TimeUnit.MINUTES);

    }

    /**
     * 将 hash，key，value 存放到redis数据库中，设置过期时间单位是分钟
     *
     * @param hash
     * @param key
     * @param value
     * @param expireTime 单位是秒
     */
    public void put(String hash, String key, T value, long expireTime) {
        HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(hash, key, value);
        redisTemplate.expire(key, expireTime, TimeUnit.MINUTES);

    }

    /**
     * 判断 key 是否在 redis 数据库中
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }


    /**
     * 获取 key 对应的字符串
     *
     * @param key
     * @return
     */
    public T get(String key) {
        ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }

    /**
     * 获取 hash，key 对应的字符串
     *
     * @param hash
     * @param key
     * @return
     */
    public T get(String hash, String key) {
        HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(hash, key);
    }

    /**
     * 获得 key 对应的键值，并更新缓存时间，时间长度为默认值
     *
     * @param key
     * @return
     */
    public T getAndUpdateTime(String key) {
        T result = get(key);
        if (result != null) {
            set(key, result);
        }
        return result;
    }

    /**
     * 删除 key 对应的 value
     *
     * @param key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }

}
